package dk.dashboardFP.elements.button {
	import dk.dashboardFP.elements.IElement;
	import dk.dashboardFP.interactionElementFramwork.InteractionElement;
	import dk.dashboardFP.interactionElementFramwork.InteractionElementView;
	import dk.dashboardFP.interactionElementFramwork.buttons.AbstractButton;
	import dk.dashboardFP.interactionElementFramwork.buttons.ButtonEvent;
	import dk.dashboardFP.panels.ui.text.DFPLabel;

	import flash.display.Sprite;

	/**
	 * @author josef
	 */
	public class ButtonElement extends Sprite implements IElement {
		
		private var _sLabel : String;
		private var _sPreLabel : String;
		private var _doTitle : DFPLabel;
		
		protected var _oButton : InteractionElement;
		private var _oID : Object;

		public function ButtonElement(oID:Object, sLabel:String="", sPreLabel:String="") {
			
			_oID = oID;
			
			_sPreLabel = sPreLabel;
			_sLabel = sLabel;
			
			setupElements();
		}
		
		protected function setupElements() : void {
			
			var intCountPosX:int=4;
			var intCountPosY:int=7;
			
			//:::::::
			// pre label 
			if (_sPreLabel!="") {
				
				_doTitle=new DFPLabel(_sPreLabel, true);
				addChild(_doTitle);
				
				_doTitle.x = intCountPosX;
				_doTitle.y = intCountPosY;
				
				intCountPosX += _doTitle.width+5;
			}
			
			//:::::::
			// button
			_oButton=get_oInteractionElement(_sLabel, oID);
			addChild(_oButton.doView);
			
			_oButton.addEventListener(ButtonEvent.CLICKED, onButtonClicked, false, 0, true);
			 
			_oButton.x = intCountPosX;
			_oButton.y = intCountPosY;
		}

		private function onButtonClicked(event : ButtonEvent) : void {
			dispatchEvent(new ButtonElementEvent(ButtonElementEvent.CLICKED, true));
		}

		public function get nBottomLine() : Number {
			return _oButton.doView.height+5;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// factory
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		protected function get_oInteractionElement(sLabel : String, oIDPass:Object) : InteractionElement {
			return new AbstractButton(get_doButtonView(sLabel), oIDPass);
		}

		protected function get_doButtonView(sLabel:String, nWidth:Number=NaN) : InteractionElementView {
			return new DFPButtonView(sLabel, nWidth);
		}
		
		public function get oID() : Object {
			return _oID;
		}
		
		public function get oButton() : InteractionElement {
			return _oButton;
		}
	}
}
